package com.example.wto.viewpagertest01;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends FragmentActivity
{

    private ViewPagerIndicator mPagerIndicator;
    private ViewPager mViewPager;
    private List<String> mTitles= Arrays.asList("教师","课程","教室","班级");
    private List<Fragment> mListPage=new ArrayList<>();
    private FragmentPagerAdapter mPagerAdapter;
    private TextView mTab_teacher;
    private TextView mTab_course;
    private TextView mTab_room;
    private TextView mTab_class;
    private List<TextView> mTabs;
    private final String TAB_COLOR_NO_SELECTED="#FFFFFF";
    //private final String TAB_COLOR_SELECTED="#F8D61D";
    private final String TAB_COLOR_SELECTED="#FAE371";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initDatas();
        Log.i("mainactity",mTitles.toString());
        mViewPager.setAdapter(mPagerAdapter);
        //设置tab的点击事件
        setTabItemOnclick();
    }
    public void initViews()
    {
        //加载顶部tab到list中
        mTab_teacher=(TextView)findViewById(R.id.id_indicator_teacher);
        mTab_course=(TextView)findViewById(R.id.id_indicator_course);
        mTab_room=(TextView)findViewById(R.id.id_indicator_room);
        mTab_class=(TextView)findViewById(R.id.id_indicator_class);
        mTabs=new ArrayList<TextView>();
        mTabs.add(mTab_teacher);
        mTabs.add(mTab_course);
        mTabs.add(mTab_room);
        mTabs.add(mTab_class);

        mViewPager=(ViewPager) this.findViewById(R.id.id_view_pager);
        mPagerIndicator=(ViewPagerIndicator)findViewById(R.id.id_pager_indicator);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {

            }

            @Override
            public void onPageSelected(int position)
            {
                highLightTabColor(position);
            }

            @Override
            public void onPageScrollStateChanged(int state)
            {

            }
        });
    }
    public void initDatas()
    {
        mListPage.add(VpTeacherFragement.newInstance(mTitles.get(0)));
        mListPage.add(VpLessonFragement.newInstance(mTitles.get(1)));
        mListPage.add(VpClassFragement.newInstance(mTitles.get(2)));
        mListPage.add(VpRoomFragement.newInstance(mTitles.get(3)));

        //将第一个tab标签高亮
        highLightTabColor(0);
        mPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager())
        {
            @Override
            public Fragment getItem(int position)
            {
                return mListPage.get(position);
            }

            @Override
            public int getCount()
            {
                return mListPage.size();
            }
        };
    }
    public void resetTabColor()
    {
        for(int i=0;i<mTabs.size();i++)
        {
            mTabs.get(i).setTextColor(Color.parseColor(TAB_COLOR_NO_SELECTED));
        }
    }
    public void highLightTabColor(int position)
    {
        resetTabColor();
        mTabs.get(position).setTextColor(Color.parseColor(TAB_COLOR_SELECTED));
    }
    private void setTabItemOnclick()
    {
        for(int i=0;i<mTabs.size();i++)
        {
            final int j=i;
            mTabs.get(i).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    mViewPager.setCurrentItem(j);
                }
            });
        }
    }
}
